package com.mystnihon.freeremote.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.SimpleOnPageChangeListener;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.mystnihon.freeremote.FreeRemoteApplication;
import com.mystnihon.freeremote.R;
import com.mystnihon.freeremote.ui.fragment.GeneralFragment;
import com.mystnihon.freeremote.ui.fragment.SeedboxFragment;

public class FreeRemoteActivity extends SherlockFragmentActivity implements ActionBar.TabListener {

	private static int NUM_ITEMS = 2;

	private MyPagerAdapter mAdapter;
	private ViewPager mPager;
	private MyPageListener mMyPageSelected;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		initSherlockActivity();

	}

	@Override
	protected void onStart() {
		if (!FreeRemoteApplication.spiceManager.isStarted())
			FreeRemoteApplication.spiceManager.start(this);
		// getMyApplication().getGuideManager().getChannelForToday();
		super.onStart();
	}

	@Override
	protected void onDestroy() {
		if (FreeRemoteApplication.spiceManager.isStarted())
			FreeRemoteApplication.spiceManager.shouldStop();
		super.onDestroy();
	}

	private void initSherlockActivity() {
		mAdapter = new MyPagerAdapter(getSupportFragmentManager());
		mMyPageSelected = new MyPageListener(getSupportActionBar());
		mPager = (ViewPager) findViewById(R.id.fr_pager);
		mPager.setAdapter(mAdapter);
		mPager.setOnPageChangeListener(mMyPageSelected);

		getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		String[] tabstext = getResources().getStringArray(R.array.tab_content);
		for (int i = 0; i < NUM_ITEMS; i++) {
			ActionBar.Tab tab = getSupportActionBar().newTab();
			tab.setText(tabstext[i]);
			tab.setTabListener(this);
			getSupportActionBar().addTab(tab);
		}
	}

	private class MyPagerAdapter extends FragmentStatePagerAdapter {

		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return NUM_ITEMS;
		}

		@Override
		public Fragment getItem(int position) {

			switch (position) {
			case 0:
				return GeneralFragment.newInstance();
			case 1:
				return SeedboxFragment.newInstance();
			}
			return null;
		}
	}

	class MyPageListener extends SimpleOnPageChangeListener {
		ActionBar mActionbar;

		public MyPageListener(ActionBar actionbar) {
			mActionbar = actionbar;
		}

		@Override
		public void onPageSelected(int position) {
			mActionbar.setSelectedNavigationItem(position);

		}

	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		mPager.setCurrentItem(tab.getPosition(), true);

	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {

	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {

	}

}